<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

/*数字资源用户访问记录表model*/

class ResourceTotalAccessNumUser extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';



    protected $table = 'resource_total_access_num_user';

    /**
     * 记录浏览量
     * @param resource_guid 资源guid
     * @param resource_business_id 资源商id
     * @param big_type_id  资源大类id
     * @param browse_num  浏览量
     * @return void
     */
    public function addAccessNumUser($user_id, $resource_guid, $resource_business_id, $big_type_id, $browse_num = 1)
    {
        if (empty($user_id)) {
            return false;
        }
        $browse = $this->where('resource_guid', $resource_guid)->where('resource_business_id', $resource_business_id)->where('big_type_id', $big_type_id)->where('user_id', $user_id)->first();
        if ($browse) {
            $browse->number = $browse->number + $browse_num; //访问次数
            $browse->change_time = date('Y-m-d H:i:s');
            $browse->save();
        } else {
            $this->resource_guid = $resource_guid;
            $this->resource_business_id = $resource_business_id;
            $this->big_type_id = $big_type_id;
            $this->user_id = $user_id;
            $this->number = $browse_num;
            $this->save();
        }
    }

    /**
     * 获取浏览记录列表
     * @param resource_business_id 资源商id
     * @param big_type_id  资源大类id
     * @param user_id  用户id
     * @param limit int 分页大小
     */
    public function getUserAccessNumResourceList($user_id, $resource_business_id = null, $big_type_id = null, $limit = 10)
    {
        if ($big_type_id == 999) {
            return null;
        }

        //获取数据
        $field = ['i.id', 'i.resource_guid', 'r.resource_business_id', 'i.change_time', 'r.big_type_id', 'r.title', 'r.author', 'r.isbn', 'r.press', 'r.pre_time', 'r.img', 'r.intro'];
        $res = $this->from($this->getTable() . " as i")->select($field)
            ->join('resource_total as r', 'r.guid', '=', 'i.resource_guid')
            ->join('resource_business_name as n', 'n.id', '=', 'r.resource_business_id')
            ->where(function ($query) use ($user_id, $resource_business_id, $big_type_id) {
                if ($user_id) {
                    $query->where('i.user_id', $user_id);
                }
                if ($resource_business_id) {
                    $query->where('i.resource_business_id', $resource_business_id);
                }
                if ($big_type_id) {
                    $query->where('i.big_type_id', $big_type_id);
                }
            })
            ->where('r.is_del', 1)
            ->where('n.is_del', 1)
            ->orderByDesc('i.change_time')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 删除浏览记录
     * @param ids 对应浏览记录id  多个逗号拼接
     * @param user_id  用户id
     */
    public function delAccessNumUser($ids, $user_id)
    {
        $ids = explode(',', $ids);
        $res = $this->where('user_id', $user_id)->whereIn('id', $ids)->delete();
        return $res;
    }
}
